import dolphindb as ddb
import pandas as pd
from utilities.correlation_analysis import find_high_correlation_periods, process_combinations
from sklearn.preprocessing import StandardScaler
import time
import matplotlib.pyplot as plt

start_time = time.time()
s = ddb.session("192.168.200.179", 8832, "chenzhimin", "suhhgjy98y_JHg87")

code_list = '"600866.SH",  "603222.SH",  "002481.SZ"'

start_date = "2024.01.29"
end_date = "2024.03.18"

listed_company_list_path = r'F:\Personal\data\merged\listed_company.csv'



basic_info_sql = """
select id, secu_code as ts_code, secu_abbr, secu_market, listed_sector, chi_spelling, listed_date from loadTable('dfs://XBBASE_listed_company_list', 'listed_company_list') 

"""
query_sql = basic_info_sql

df = s.run(query_sql)

df.to_csv(listed_company_list_path, index=False)
# # 确保trading_date是日期类型
# df['trading_date'] = pd.to_datetime(df['trading_date'])
#
# # 创建一个新的列来存储标准化后的值
# df['factor_value_scaled'] = 0
#
# # 对每个secu_code分组，并应用StandardScaler
# for name, group in df.groupby('secu_code'):
#     scaler = StandardScaler()
#     scaled_factor_values = scaler.fit_transform(group['factor_value'].values.reshape(-1, 1))
#     df.loc[group.index, 'factor_value_scaled'] = scaled_factor_values.flatten()
#
# # 绘制标准化后的factor_value折线图，按secu_code分组
# plt.figure(figsize=(10, 6))  # 可选：设置图形大小
# for name, group in df.groupby('secu_code'):
#     plt.plot(group['trading_date'], group['factor_value_scaled'], label=name)
#
# # 设置图例、标签和标题
# plt.legend()
# plt.xlabel('Trading Date')
# plt.ylabel('Factor Value (Scaled)')
# plt.title('Factor Value over '
#           'Time by secu_code (Scaled)')
#
# # 显示图形
# plt.show()
